using System;
using System.Xml.Linq;

namespace NetQueryLib.Expressions
{
	internal class IDSelectorExpression : SelectorExpression
	{
		public IDSelectorExpression(string selector)
			: base(selector)
		{
		}

		private string ElementName
		{
			get
			{
				return Selector.Substring(0, Selector.IndexOf("#"));
			}
		}

		private string ElementID
		{
			get
			{
				return Selector.Substring(Selector.IndexOf("#") + 1);
			}
		}

		public override bool Filter(XElement element)
		{
			var elementName = String.IsNullOrEmpty(ElementName) ? element.Name.LocalName : ElementName;
			var id = element.Attribute("id");

			if(id == null)
				return false;

			return elementName == element.Name.LocalName && id.Value == ElementID;
		}
	}
}